package com.zrwl.poscloud.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.CompanyBusinessFeeConfig;
import com.zrwl.poscloud.base.entity.ImportBatch;
import com.zrwl.poscloud.base.enums.EIBatchType;
import com.zrwl.poscloud.base.enums.ESourceType;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.JsonUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.ImportBatchMapper;
import com.zrwl.poscloud.xo.service.AdminLogService;
import com.zrwl.poscloud.xo.service.CompanyBusinessFeeConfigService;
import com.zrwl.poscloud.xo.service.ImportBatchService;
import com.zrwl.poscloud.xo.vo.AdminLogVO;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.ImportBatchVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 导入批次 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-11-02
 */
@Service
public class ImportBatchServiceImpl extends SuperServiceImpl<ImportBatchMapper, ImportBatch> implements ImportBatchService {
    /***
     * 批量操作类型固定为 5
     */
    final static int adminLogType = 5;
    @Autowired
    private ImportBatchService importBatchService;
    @Autowired
    private CompanyBusinessFeeConfigService companyBusinessFeeConfigService;
    @Autowired
    private AdminServiceImpl adminService;
    @Autowired
    private AdminLogService adminLogService;

    @Override
    public IPage<ImportBatch> getPageList(ImportBatchVO importBatchVO) {
        QueryWrapper<ImportBatch> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(importBatchVO.getKeyword()) && !StringUtils.isEmpty(importBatchVO.getKeyword().trim())) {
            queryWrapper.like("name", importBatchVO.getKeyword().trim());
        }
        if (importBatchVO.getAdminUid() != null && !("").equals(importBatchVO.getAdminUid())) {
            queryWrapper.eq("admin_uid", importBatchVO.getAdminUid());
        }
        if (importBatchVO.getType() != 0) {
            queryWrapper.eq("type", importBatchVO.getType());
        }
        Page<ImportBatch> page = new Page<>();
        page.setCurrent(importBatchVO.getCurrentPage());
        page.setSize(importBatchVO.getPageSize());
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<ImportBatch> pageList = importBatchService.page(page, queryWrapper);
        List<ImportBatch> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<ImportBatch> getAllList(ImportBatchVO importBatchVO) {
        QueryWrapper<ImportBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<ImportBatch> list = importBatchService.list(queryWrapper);
        return list;
    }

    @Override
    public String add(ImportBatchVO importBatchVO) {

        switch (EIBatchType.getByValue(importBatchVO.getType())) {
            case employeeAccountOpen:
                importBatchVO.setColumnConfig("{\"column1\":\"上级机构,非空|格式**/**\",\"column2\":\"所属机构,非空\",\"column3\":\"人员编号,去重|默认身份证号获取|系统自动生成\",\"column4\":\"姓名,非空\",\"column5\":\"身份证号,校验|去重\",\"column6\":\"性别,校验|默认身份证号获取\",\"column7\":\"生日,校验|默认身份证号获取|日期格式\",\"column8\":\"进入时间,非空|时间格式\",\"column10\":\"人脸支付,关/开|默认关\",\"column11\":\"消费等级,|非空\",\"column12\":\"日限额,非空\",\"column13\":\"月限额,非空|校验\",\"column14\": \"超限额密码,默认编号后六位\"}");
                break;
            case deptBatch:
                importBatchVO.setColumnConfig("{\"column1\":\"上级机构,非空|格式**/**\",\"column2\": \"所属机构,非空\",\"column3\":\"导入新机构名称,非空|去重\"}");
                break;
            case employeeAccountEdit:
                importBatchVO.setColumnConfig("{\n" +
                        "    \"column1\": \"上级机构,非空|格式**/**\",\n" +
                        "    \"column2\": \"所属机构,非空\",\n" +
                        "    \"column3\": \"人员编号,非空\",\n" +
                        "    \"column4\": \"姓名,非空\",\n" +
                        "    \"column5\": \"身份证号,校验|去重]\",\n" +
                        "    \"column6\": \"性别,校验|默认身份证号获取\",\n" +
                        "    \"column7\": \"生日,默认身份证号获取|日期格式\",\n" +
                        "    \"column8\": \"进入时间,非空|时间格式\",\n" +
                        "    \"column10\": \"人脸支付,关/开|默认关\",\n" +
                        "    \"column11\": \"消费等级,非空\",\n" +
                        "    \"column12\": \"日限额,非空\",\n" +
                        "    \"column13\": \"月限额,非空|校验\"\n" +
                        "    \"column14\": \"超限额密码,默认编号后六位\"\n" +
                        "}");
                break;
            case icCardOpen:
                importBatchVO.setColumnConfig("{\n" +
                        "    \"column1\": \"上级机构,校验|格式**/**\",\n" +
                        "    \"column2\": \"所属机构,校验\",\n" +
                        "    \"column3\": \"人员编号,非空\",\n" +
                        "    \"column4\": \"姓名,校验\",\n" +
                        "    \"column5\": \"身份证号,校验\",\n" +
                        "    \"column6\": \"卡序列号,非空|去重\",\n" +
                        "    \"column7\": \"卡备注名称\",\n" +
                        "    \"column8\": \"开卡充值金额\",\n" +
                        "    \"column9\": \"卡片类型,|消费卡\",\n" +
                        "    \"column10\": \"收费方式,|非空\", \n" +
                        "    \"column11\": \"超限额密码,默认编号后六位\"\n" +
                        "}");
                List<CompanyBusinessFeeConfig> cardOpeningFee = companyBusinessFeeConfigService.getFeeConfigMyCompany(adminService.getMe(),1);
                importBatchVO.setParam(JsonUtils.objectToJson(cardOpeningFee));
                break;
            case icCardEdit:
                importBatchVO.setColumnConfig(" {\n" +
                        "    \"column1\": \"上级机构,非空|格式**/**\",\n" +
                        "    \"column2\": \"所属机构,非空\",\n" +
                        "    \"column3\": \"人员编号,非空\",\n" +
                        "    \"column4\": \"姓名\",\n" +
                        "    \"column5\": \"身份证号,校验\",\n" +
                        "    \"column6\": \"卡序列号,非空\",\n" +
                        "    \"column7\": \"卡备注名称\",\n" +
                        "    \"column8\": \"日限额,非空\",\n" +
                        "    \"column9\": \"月限额,非空|校验\",\n" +
                        "    \"column10\": \"账户有效期\",\n" +
                        "    \"column11\": \"超限额密码,默认编号后六位\"\n" +
                        "}");
                break;
        }
        ImportBatch entity = new ImportBatch();
        BeanUtils.copyProperties(importBatchVO, entity, SysConf.STATUS);
        entity.insert();
        importBatchVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(ImportBatchVO importBatchVO) {
        ImportBatch entity = importBatchService.getById(importBatchVO.getUid());
        BeanUtils.copyProperties(importBatchVO, entity);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(ImportBatchVO importBatchVO) {
        ImportBatch entity = importBatchService.getById(importBatchVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public void createAdminLog(String keyword1, ImportBatch importBatch,AdminLogVO.OPERATIONTYPE operationType, AdminVO adminVO, Object oldData, Object newData,int businessCount,int resEmployeeCount,int resCardCount) {
        //创建日志
        try {
            AdminLogVO adminLogVO = new AdminLogVO(
                    AdminLogVO.LOGLEVEL.REPORT,
                    adminLogType,
                    operationType,
                    importBatch.getSummary(),
                    oldData, newData,
                    keyword1,//业务名称
                    null,null,null,
                    0d,0d,0d,0d,0d,0d,
                    businessCount,//业务条数
                    resEmployeeCount,//人员资源
                    resCardCount);//卡片资源
            adminLogVO.setAdminUid(adminVO.getUid());
            adminLogVO.setName(adminVO.getNickName());
            adminLogVO.setSourceType(ESourceType.icl_importConfig.getType());
            adminLogVO.setSourceName(ESourceType.icl_importConfig.getName());
            adminLogVO.setSourceUid(importBatch.getUid());
            adminLogService.log(adminLogVO);
        } catch (Exception e) {
            log.error("导入数据批次-日志异常", e);
            throw new RuntimeException("导入数据批次-日志异常");
        }
    }
}
